package org.ourpioneer.excel.excute.process;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.ourpioneer.excel.excute.entity.ToEntity;

public class WriteFile {

	/**
	 * 创建2003文件的方法
	 * 
	 * @param filePath
	 */
	public void generateExcel2003(String filePath, List<Object> allInfo) {
		@SuppressWarnings("unchecked")
		List<ToEntity> toEntitylist = (List<ToEntity>) allInfo.get(0);
		@SuppressWarnings("unchecked")
		ArrayList<String> projectNameList = (ArrayList<String>) allInfo.get(1);
		@SuppressWarnings("unchecked")
		ArrayList<ArrayList<Integer>> projectLine = (ArrayList<ArrayList<Integer>>) allInfo.get(2);
		// 先创建工作簿对象
		HSSFWorkbook workbook2003 = new HSSFWorkbook();
		// 创建工作表对象并命名
		HSSFSheet sheet = workbook2003.createSheet("全表");
		//TODO:写表头
		// 遍历集合对象创建行和单元格
		for (int i = 2; i < ( toEntitylist.size() + 2 ); i++) {
			// 取出对象
			ToEntity toEntity = toEntitylist.get(i-2);
			// 创建行
			HSSFRow row = sheet.createRow(i);
			// 开始创建单元格并赋值
			HSSFCell dms_full_path = row.createCell(0);
			dms_full_path.setCellValue(toEntity.getDms_full_path());
			HSSFCell dms_name_en = row.createCell(1);
			dms_name_en.setCellValue(toEntity.getDms_name_en());
			HSSFCell dms_doc_no = row.createCell(2);
			dms_doc_no.setCellValue(toEntity.getDms_doc_no());
			HSSFCell dms_title = row.createCell(3);
			dms_title.setCellValue(toEntity.getDms_title());
			HSSFCell dms_year = row.createCell(4);
			dms_year.setCellValue(toEntity.getDms_year());
			HSSFCell dms_project_no = row.createCell(5);
			dms_project_no.setCellValue(toEntity.getDms_project_no());
			HSSFCell dms_project_zh = row.createCell(6);
			dms_project_zh.setCellValue(toEntity.getDms_project_zh());
			HSSFCell dms_project_en = row.createCell(7);
			dms_project_en.setCellValue(toEntity.getDms_project_en());
			HSSFCell dms_stage = row.createCell(8);
			dms_stage.setCellValue(toEntity.getDms_stage());
			HSSFCell dms_prof_zh = row.createCell(9);
			dms_prof_zh.setCellValue(toEntity.getDms_prof_zh());
			HSSFCell dms_version = row.createCell(10);
			dms_version.setCellValue(toEntity.getDms_version());
			HSSFCell dms_language = row.createCell(11);
			dms_language.setCellValue(toEntity.getDms_language());
			HSSFCell object_type = row.createCell(12);
			object_type.setCellValue(toEntity.getObject_type());
			HSSFCell dms_doc_lifecycle = row.createCell(13);
			dms_doc_lifecycle.setCellValue(toEntity.getDms_doc_lifecycle());
			HSSFCell dms_doc_lifecycle_label = row.createCell(14);
			dms_doc_lifecycle_label.setCellValue(toEntity.getDms_doc_lifecycle_label());
			HSSFCell dms_volume_type = row.createCell(15);
			dms_volume_type.setCellValue(toEntity.getDms_volume_type());
			HSSFCell dms_category_code = row.createCell(16);
			dms_category_code.setCellValue(toEntity.getDms_category_code());
			HSSFCell dms_fondsid = row.createCell(17);
			dms_fondsid.setCellValue(toEntity.getDms_fondsid());
			HSSFCell dms_volume_state = row.createCell(18);
			dms_volume_state.setCellValue(toEntity.getDms_volume_state());
			HSSFCell dms_volume_state_label = row.createCell(19);
			dms_volume_state_label.setCellValue(toEntity.getDms_volume_state_label());
			HSSFCell dms_type = row.createCell(20);
			dms_type.setCellValue(toEntity.getDms_type());
			HSSFCell dms_archival_code = row.createCell(21);
			dms_archival_code.setCellValue(toEntity.getDms_archival_code());
			HSSFCell dms_responsible = row.createCell(22);
			dms_responsible.setCellValue(toEntity.getDms_responsible());
			HSSFCell dms_retention_period = row.createCell(23);
			dms_retention_period.setCellValue(toEntity.getDms_retention_period());
			HSSFCell dms_carrier_type = row.createCell(24);
			dms_carrier_type.setCellValue(toEntity.getDms_carrier_type());
			HSSFCell dms_security_level = row.createCell(25);
			dms_security_level.setCellValue(toEntity.getDms_security_level());
			HSSFCell dms_archive_date = row.createCell(26);
			dms_archive_date.setCellValue(toEntity.getDms_archive_date());
			HSSFCell dms_archive_dept = row.createCell(27);
			dms_archive_dept.setCellValue(toEntity.getDms_archive_dept());
			HSSFCell dms_remark = row.createCell(28);
			dms_remark.setCellValue(toEntity.getDms_remark());
		}
		for (int j = 0; j < projectNameList.size(); j++) {
			String sheetName = projectNameList.get(j);
			sheetName.replace(":", " ");
			sheetName.replace("/", " ");
			sheetName.replace("?", " ");
			sheetName.replace("*", " ");
			sheetName.replace("[", " ");
			sheetName.replace("]", " ");
			if(sheetName.length()>31){
				sheetName = sheetName.subSequence(0, 30).toString();
			}
			HSSFSheet sheetSub = workbook2003.createSheet(sheetName);
			//TODO:写表头
			// 遍历集合对象创建行和单元格
			ArrayList<Integer> line = projectLine.get(j);
			for (int i = 2; i < ( line.size() + 2 ); i++) {
				// 取出对象
				ToEntity toEntity = toEntitylist.get(line.get(i-2));
				// 创建行
				HSSFRow row = sheetSub.createRow(i);
				// 开始创建单元格并赋值
				HSSFCell dms_full_path = row.createCell(0);
				dms_full_path.setCellValue(toEntity.getDms_full_path());
				HSSFCell dms_name_en = row.createCell(1);
				dms_name_en.setCellValue(toEntity.getDms_name_en());
				HSSFCell dms_doc_no = row.createCell(2);
				dms_doc_no.setCellValue(toEntity.getDms_doc_no());
				HSSFCell dms_title = row.createCell(3);
				dms_title.setCellValue(toEntity.getDms_title());
				HSSFCell dms_year = row.createCell(4);
				dms_year.setCellValue(toEntity.getDms_year());
				HSSFCell dms_project_no = row.createCell(5);
				dms_project_no.setCellValue(toEntity.getDms_project_no());
				HSSFCell dms_project_zh = row.createCell(6);
				dms_project_zh.setCellValue(toEntity.getDms_project_zh());
				HSSFCell dms_project_en = row.createCell(7);
				dms_project_en.setCellValue(toEntity.getDms_project_en());
				HSSFCell dms_stage = row.createCell(8);
				dms_stage.setCellValue(toEntity.getDms_stage());
				HSSFCell dms_prof_zh = row.createCell(9);
				dms_prof_zh.setCellValue(toEntity.getDms_prof_zh());
				HSSFCell dms_version = row.createCell(10);
				dms_version.setCellValue(toEntity.getDms_version());
				HSSFCell dms_language = row.createCell(11);
				dms_language.setCellValue(toEntity.getDms_language());
				HSSFCell object_type = row.createCell(12);
				object_type.setCellValue(toEntity.getObject_type());
				HSSFCell dms_doc_lifecycle = row.createCell(13);
				dms_doc_lifecycle.setCellValue(toEntity.getDms_doc_lifecycle());
				HSSFCell dms_doc_lifecycle_label = row.createCell(14);
				dms_doc_lifecycle_label.setCellValue(toEntity.getDms_doc_lifecycle_label());
				HSSFCell dms_volume_type = row.createCell(15);
				dms_volume_type.setCellValue(toEntity.getDms_volume_type());
				HSSFCell dms_category_code = row.createCell(16);
				dms_category_code.setCellValue(toEntity.getDms_category_code());
				HSSFCell dms_fondsid = row.createCell(17);
				dms_fondsid.setCellValue(toEntity.getDms_fondsid());
				HSSFCell dms_volume_state = row.createCell(18);
				dms_volume_state.setCellValue(toEntity.getDms_volume_state());
				HSSFCell dms_volume_state_label = row.createCell(19);
				dms_volume_state_label.setCellValue(toEntity.getDms_volume_state_label());
				HSSFCell dms_type = row.createCell(20);
				dms_type.setCellValue(toEntity.getDms_type());
				HSSFCell dms_archival_code = row.createCell(21);
				dms_archival_code.setCellValue(toEntity.getDms_archival_code());
				HSSFCell dms_responsible = row.createCell(22);
				dms_responsible.setCellValue(toEntity.getDms_responsible());
				HSSFCell dms_retention_period = row.createCell(23);
				dms_retention_period.setCellValue(toEntity.getDms_retention_period());
				HSSFCell dms_carrier_type = row.createCell(24);
				dms_carrier_type.setCellValue(toEntity.getDms_carrier_type());
				HSSFCell dms_security_level = row.createCell(25);
				dms_security_level.setCellValue(toEntity.getDms_security_level());
				HSSFCell dms_archive_date = row.createCell(26);
				dms_archive_date.setCellValue(toEntity.getDms_archive_date());
				HSSFCell dms_archive_dept = row.createCell(27);
				dms_archive_dept.setCellValue(toEntity.getDms_archive_dept());
				HSSFCell dms_remark = row.createCell(28);
				dms_remark.setCellValue(toEntity.getDms_remark());
			}
		}
		// 生成文件
		File file = new File(filePath);
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			workbook2003.write(fos);
			workbook2003.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

}
